package com.myl.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myl.admin.service.ISysRoleService;
import com.myl.core.domain.entity.SysRole;
import com.myl.core.domain.model.TablePageList;
import com.myl.core.exception.ServiceExceptionHandler;
import com.myl.core.mapper.SysRoleMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 后台角色表  服务实现类
 * </p>
 *
 * @author Myl <ixiaomu@qq.com>
 * @since 2024-01-10 11:21
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    private SysRoleMapper sysRoleMapper;

    /**
     *  <p>
     *   列表带分页数据
     *  </p>
     *
     * @param current  当前页码
     * @param pageSize 分页条数
     * @param keyword 关键词
     * @return Page
     */
    @Override
    public TablePageList getListWithPage(Integer current, Integer pageSize, String keyword) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<SysRole>();
        // 条件筛选
        if (!"".equals(keyword)) {
            wrapper.like(SysRole::getName, keyword);
        }
        wrapper.lt(SysRole::getCreateTime, new Date());
        // 排序
        wrapper.orderByAsc(SysRole::getCreateTime);
        // 分页数据
        Page<SysRole> page = page(new Page<>(
                current,
                pageSize
        ), wrapper);
        // 输出表格分页数据
        return new TablePageList(page);
    }
    /**
     *  <p>
     *   详情
     *  </p>
     *
     * @return SysRole
     */
    @Override
    public SysRole getInfo(Integer id) {

        SysRole info = getById(id);
        // TODO 设置隐藏字段


        return info;
    }

    /**
     *  <p>
     *   新增或更新数据
     *  </p>
     * @param entity SysRole 用户信息
     * @return User
     */
    @Override
    public SysRole saveOrUpdateById(SysRole entity) {
        boolean result;
        if (null == entity.getId()) {
            result = save(entity);
        } else {
            result = updateById(entity);
        }
        if (!result) {
            throw new ServiceExceptionHandler("数据入库失败！" );
        }
        return entity;
    }

    /**
    *  <p>
    *      批量删除
    *  </p>
    * @param ids ID集合
    * @return boolean
    */
    @Override
    public boolean deleteBatchByIds(List<Integer> ids) {
        return removeBatchByIds(ids);
    }

    @Override
    public boolean checkFieldUnique(SysRole entity, String field_name, String field_value) {
//        Long menuId = StringUtils.isNull(entity.getId()) ? -1L : entity.getId();
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.eq(field_name, field_value);
        if (entity.getId() != null){
            wrapper.ne("id", entity.getId());
        }
        long count = count(wrapper);
        return count > 0;
    }
}
